package com.qiyi.player.core.video.provider.clientp2pprovider
{
   import flash.events.EventDispatcher;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.core.model.IMovie;
   import com.qiyi.player.core.video.decoder.IDecoder;
   import com.qiyi.player.core.video.file.clientp2pfile.IClientP2PFile;
   import com.qiyi.player.core.video.provider.actors.clientp2p.ClientP2PEngine;
   import com.qiyi.player.core.video.file.actors.datatile.IFlvBlock;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.core.model.impls.Segment;
   import com.qiyi.player.core.model.impls.Keyframe;
   import com.qiyi.player.core.model.events.MovieEvent;
   import com.qiyi.player.core.video.events.ClientP2PEvent;
   import com.adobe.serialization.json.JSON;
   import flash.utils.ByteArray;
   import com.qiyi.player.core.video.file.actors.datatile.FlvBlock;
   import com.qiyi.player.core.video.def.DataSourceTypeEnum;
   import com.qiyi.player.core.video.events.ProviderEvent;
   import com.qiyi.player.base.logging.Log;
   
   public class ClientP2PProvider extends EventDispatcher implements IClientP2PProvider
   {
      
      public function ClientP2PProvider(param1:IClientP2PFile, param2:IDecoder) {
         this._log = Log.getLogger("com.qiyi.player.core.video.provider.clientp2pprovider.ClientP2PProvider");
         super();
         this._file = param1;
         this._decoder = param2;
         this._holder = this._file.holder;
         this._movie = this._file.movie;
         this._movie.addEventListener(MovieEvent.Evt_Ready,this.onMovieReady);
         this._movie.addEventListener(MovieEvent.Evt_Meta_Ready,this.onMovieMetaReady);
         this._clientP2PEngine = this._holder.clientP2PEngine;
         this._clientP2PEngine.addEventListener(ClientP2PEvent.Evt_Connected,this.onClientP2PConnected);
         this._clientP2PEngine.addEventListener(ClientP2PEvent.Evt_Start_LoadOK,this.onClientP2PStartLoadOK);
         this._clientP2PEngine.addEventListener(ClientP2PEvent.Evt_ReceiveVideo,this.onClientP2PReceiveVideo);
         this._clientP2PEngine.addEventListener(ClientP2PEvent.Evt_Failed,this.onClientP2PFailed);
         if(this._clientP2PEngine.isOpen)
         {
            this._delaySetVRS = false;
            this._clientP2PEngine.sendSetVRS(com.adobe.serialization.json.JSON.encode(this._movie.source));
            this._delaySetMeta = false;
            if(this._movie.ready)
            {
               if(this._movie.curDefinition.metaIsReady)
               {
                  this._clientP2PEngine.sendSetMeta(this._movie.curDefinition.type.id,this._movie.curAudioTrack.type.id,XML(this._movie.curDefinition.meta).toXMLString());
               }
               else
               {
                  this._clientP2PEngine.sendSetMeta(this._movie.curDefinition.type.id,this._movie.curAudioTrack.type.id,"");
               }
            }
         }
         else
         {
            this._delaySetVRS = true;
            this._delaySetMeta = true;
         }
         this._log.debug("ClientP2PProvider has been created!");
      }
      
      private const FILE_LENGTH_BYTE_SIZE:int = 4;
      
      private const SEGMENT_INDEX_BYTE_SIZE:int = 1;
      
      private var _holder:ICorePlayer;
      
      private var _movie:IMovie;
      
      private var _decoder:IDecoder;
      
      private var _file:IClientP2PFile;
      
      private var _clientP2PEngine:ClientP2PEngine;
      
      private var _delaySeeked:Boolean = false;
      
      private var _delaySetVRS:Boolean = false;
      
      private var _delaySetMeta:Boolean = false;
      
      private var _loadingFailed:Boolean = false;
      
      private var _isAllowReceive:Boolean = false;
      
      private var _receiveVideoBytes:int = -1;
      
      private var _loadingBlock:IFlvBlock;
      
      private var _startLoadTime:int = 0;
      
      private var _log:ILogger;
      
      public function get loadingFailed() : Boolean {
         return this._loadingFailed;
      }
      
      public function set openSelectPlay(param1:Boolean) : void {
      }
      
      public function stopLoading() : void {
         this._isAllowReceive = false;
         this._delaySeeked = false;
         if(this._loadingBlock)
         {
            this._loadingBlock.loading = false;
            this._loadingBlock.complete = false;
            this._loadingBlock.endAppend();
            this._loadingBlock = null;
         }
         this._receiveVideoBytes = -1;
         this._clientP2PEngine.sendStopLoad();
      }
      
      public function clear() : void {
         this.stopLoading();
      }
      
      public function prepareSwitchMediaData() : void {
         this.stopLoading();
         if(this._clientP2PEngine.isOpen)
         {
            this._delaySetMeta = false;
            if(this._movie.ready)
            {
               if(this._movie.curDefinition.metaIsReady)
               {
                  this._clientP2PEngine.sendSetMeta(this._movie.curDefinition.type.id,this._movie.curAudioTrack.type.id,XML(this._movie.curDefinition.meta).toXMLString());
               }
               else
               {
                  this._clientP2PEngine.sendSetMeta(this._movie.curDefinition.type.id,this._movie.curAudioTrack.type.id,"");
               }
            }
         }
         else
         {
            this._delaySetMeta = true;
         }
      }
      
      public function retry() : void {
         this.onFailed();
      }
      
      public function seek(param1:int) : void {
         var _loc2_:* = 0;
         var _loc3_:Segment = this._movie.getSegmentByTime(param1);
         var _loc4_:Keyframe = _loc3_.getKeyframeByTime(param1);
         if(_loc4_)
         {
            _loc2_ = _loc4_.position;
         }
         var _loc5_:int = this._file.findBlockIndexFrom(_loc3_,_loc2_);
         if(_loc5_ == -1)
         {
            if(_loc4_)
            {
               this._startLoadTime = _loc4_.time;
            }
            else
            {
               this._startLoadTime = _loc3_.startTime;
            }
            if(this._clientP2PEngine.isOpen)
            {
               this.stopLoading();
               this._clientP2PEngine.sendStartLoad(this._startLoadTime);
            }
            else
            {
               this._delaySeeked = true;
            }
         }
      }
      
      public function drive() : void {
      }
      
      public function destroy() : void {
         this._movie.removeEventListener(MovieEvent.Evt_Ready,this.onMovieReady);
         this._movie.removeEventListener(MovieEvent.Evt_Meta_Ready,this.onMovieMetaReady);
         this._clientP2PEngine.removeEventListener(ClientP2PEvent.Evt_Connected,this.onClientP2PConnected);
         this._clientP2PEngine.removeEventListener(ClientP2PEvent.Evt_Start_LoadOK,this.onClientP2PStartLoadOK);
         this._clientP2PEngine.removeEventListener(ClientP2PEvent.Evt_ReceiveVideo,this.onClientP2PReceiveVideo);
         this._clientP2PEngine.removeEventListener(ClientP2PEvent.Evt_Failed,this.onClientP2PFailed);
         this._loadingBlock = null;
         this._clientP2PEngine.sendStopLoad();
      }
      
      private function onMovieReady(param1:MovieEvent) : void {
         if(this._clientP2PEngine.isOpen)
         {
            this._delaySetMeta = false;
            if(this._movie.curDefinition.metaIsReady)
            {
               this._clientP2PEngine.sendSetMeta(this._movie.curDefinition.type.id,this._movie.curAudioTrack.type.id,XML(this._movie.curDefinition.meta).toXMLString());
            }
            else
            {
               this._clientP2PEngine.sendSetMeta(this._movie.curDefinition.type.id,this._movie.curAudioTrack.type.id,"");
            }
         }
         else
         {
            this._delaySetMeta = true;
         }
      }
      
      private function onMovieMetaReady(param1:MovieEvent) : void {
         if(this._clientP2PEngine.isOpen)
         {
            this._delaySetMeta = false;
            this._clientP2PEngine.sendSetMeta(this._movie.curDefinition.type.id,this._movie.curAudioTrack.type.id,XML(this._movie.curDefinition.meta).toXMLString());
         }
         else
         {
            this._delaySetMeta = true;
         }
      }
      
      private function onClientP2PConnected(param1:ClientP2PEvent) : void {
         if(this._delaySetVRS)
         {
            this._clientP2PEngine.sendSetVRS(com.adobe.serialization.json.JSON.encode(this._movie.source));
            this._delaySetVRS = false;
         }
         if(this._delaySetMeta)
         {
            if(this._movie.ready)
            {
               if(this._movie.curDefinition.metaIsReady)
               {
                  this._clientP2PEngine.sendSetMeta(this._movie.curDefinition.type.id,this._movie.curAudioTrack.type.id,XML(this._movie.curDefinition.meta).toXMLString());
               }
               else
               {
                  this._clientP2PEngine.sendSetMeta(this._movie.curDefinition.type.id,this._movie.curAudioTrack.type.id,"");
               }
            }
            this._delaySetMeta = false;
         }
         if(this._delaySeeked)
         {
            this._clientP2PEngine.sendStartLoad(this._startLoadTime);
            this._delaySeeked = false;
         }
      }
      
      private function onClientP2PStartLoadOK(param1:ClientP2PEvent) : void {
         this._isAllowReceive = true;
      }
      
      private function onClientP2PReceiveVideo(param1:ClientP2PEvent) : void {
         var _loc2_:Object = null;
         var _loc3_:ByteArray = null;
         var _loc4_:uint = 0;
         var _loc5_:Segment = null;
         var _loc6_:uint = 0;
         var _loc7_:* = false;
         if(this._isAllowReceive)
         {
            _loc2_ = param1.data;
            _loc3_ = new ByteArray();
            _loc3_.writeBytes(ByteArray(_loc2_.data),int(_loc2_.start),int(_loc2_.length));
            _loc3_.position = 0;
            _loc4_ = _loc3_.readUnsignedInt();
            _loc5_ = null;
            _loc6_ = _loc3_.readUnsignedByte();
            if(this._receiveVideoBytes == -1)
            {
               _loc5_ = this._movie.curDefinition.findSegmentAt(_loc6_);
               this._loadingBlock = new FlvBlock(_loc5_,_loc5_.getKeyframeByTime(this._startLoadTime),this._holder);
               this._loadingBlock.loading = true;
               this._loadingBlock.complete = false;
               this._loadingBlock.dataSourceType = DataSourceTypeEnum.ClientP2P;
               this._loadingBlock.startAppend();
               this._file.addBlock(this._loadingBlock);
               this._receiveVideoBytes = 0;
            }
            _loc7_ = this._loadingBlock.appendBytes(_loc3_);
            if(_loc7_)
            {
               this._receiveVideoBytes = this._receiveVideoBytes + (_loc3_.length - this.FILE_LENGTH_BYTE_SIZE - this.SEGMENT_INDEX_BYTE_SIZE);
               if(this._receiveVideoBytes == _loc4_)
               {
                  this._loadingBlock.loading = false;
                  this._loadingBlock.complete = true;
                  this._loadingBlock.endAppend();
                  this._loadingBlock = null;
                  if(_loc6_ != this._movie.curDefinition.segmentCount - 1)
                  {
                     _loc5_ = this._movie.curDefinition.findSegmentAt(_loc6_ + 1);
                     if(_loc5_.keyframes)
                     {
                        this._startLoadTime = _loc5_.keyframes[0].time;
                     }
                     else
                     {
                        this._startLoadTime = _loc5_.startTime;
                     }
                  }
                  this._receiveVideoBytes = -1;
               }
            }
            else
            {
               this.onFailed();
            }
         }
      }
      
      private function onClientP2PFailed(param1:ClientP2PEvent) : void {
         this.onFailed();
      }
      
      private function onFailed() : void {
         this._loadingFailed = true;
         this._clientP2PEngine.close();
         dispatchEvent(new ProviderEvent(ProviderEvent.Evt_Failed));
      }
   }
}
